#include "ui_bofugui.h"
#include "bofugui.h"

void BofuGui::writeSettings(){
    settings.setValue("filelist",ui->lineEdit_filelist->text());

    settings.beginGroup("Inventory");
    settings.setValue("inventory-name", ui->lineEdit_InventoryName->text());
    settings.setValue("language-code", ui->lineEdit_LanguageCode->text());
    settings.endGroup();

    settings.beginGroup("DATABASE");
    settings.setValue("sql-admin", ui->lineEdit_SQLAdmin->text());
    settings.setValue("admin-password", ui->lineEdit_AdminPassword->text());
    settings.setValue("database", ui->lineEdit_NewDatabase->text());
    settings.setValue("host", ui->lineEdit_Server->text());
    settings.setValue("port", ui->spinBox_Port->text());
    settings.setValue("user", ui->lineEdit_Username->text());
    settings.setValue("password", ui->lineEdit_Password->text());
    settings.endGroup();

}

//Reads all saved settings
void BofuGui::readSettings(){
    if((settings.value("filelist").toString()=="")){
        ui->lineEdit_filelist->setText("filelist.txt");
    }else{
        ui->lineEdit_filelist->setText(settings.value("filelist").toString());
    }

    settings.beginGroup("INVENTORY");
    if(settings.value("inventory-name").toString()==""){
        ui->lineEdit_InventoryName->setText("");
    } else
    {
        ui->lineEdit_InventoryName->setText(settings.value("inventory-name").toString());
    }
    if(settings.value("language-code").toString()==""){
        ui->lineEdit_InventoryName->setText("");
    } else
    {
        ui->lineEdit_LanguageCode->setText(settings.value("language-code").toString());
    }
    settings.endGroup();

    settings.beginGroup("DATABASE");
    if(settings.value("sql-admin").toString()==""){
        ui->lineEdit_SQLAdmin->setText("root");
    } else
    {
        ui->lineEdit_SQLAdmin->setText(settings.value("sql-admin").toString());
    }
    if(settings.value("admin-passwort").toString()==""){
        ui->lineEdit_AdminPassword->setText("");
    } else
    {
        ui->lineEdit_AdminPassword->setText(settings.value("admin-passwort").toString());
    }

    if(settings.value("database").toString()==""){
        ui->lineEdit_NewDatabase->setText("");
    } else
    {
        ui->lineEdit_NewDatabase->setText(settings.value("database").toString());
    }
    if(settings.value("host").toString()==""){
        ui->lineEdit_Server->setText("localhost");
    } else
    {
        ui->lineEdit_Server->setText(settings.value("host").toString());
    }
    if(settings.value("port").toInt()==0){
        ui->spinBox_Port->setValue(-1);

    } else
    {
        ui->spinBox_Port->setValue(settings.value("port").toInt());
    }
    if(settings.value("user").toString()==""){
        ui->lineEdit_Username->setText("boss");
    } else
    {
        ui->lineEdit_Username->setText(settings.value("user").toString());
    }
    if(settings.value("password").toString()==""){
        ui->lineEdit_Password->setText("");
    } else
    {
        ui->lineEdit_Password->setText(settings.value("password").toString());
    }
    settings.endGroup();
}

/*****************************************************************************************/
//Not visible at the moment

void BofuGui::on_filelistDefaultButton_2_clicked()
{
    ui->lineEdit_filelist->setText("filelist.txt");
}
void BofuGui::on_pushButton_Accept_clicked()
{
    writeSettings();

}

void BofuGui::on_pushButton_ResetAll_clicked()
{
    on_filelistDefaultButton_2_clicked();
}

